package cst.a521wh.zyfypt_521wh.Model;

import java.util.List;

import cst.a521wh.zyfypt_521wh.Service.ResService;
import cst.a521wh.zyfypt_521wh.bean.LoginBean;
import cst.a521wh.zyfypt_521wh.bean.VideoBean;
import cst.a521wh.zyfypt_521wh.iface.AmicoolListener;
import cst.a521wh.zyfypt_521wh.iface.Amicooliface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by MSI on 2017/5/29.
 */

public class ResModel<T> implements Amicooliface {
    private Retrofit retrofit;
    private ResService service;
    private static final String BASE_URL="http://amicool.neusoft.edu.cn/";

    public ResModel(String type) {
        if(type.equals("gson")){
            retrofit=new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else{
            retrofit=new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service=retrofit.create(ResService.class);

    }

    @Override
    public void login(String u, String p, final AmicoolListener l) {
        Call<LoginBean> call=service.login(u,p);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                l.onFail(t.toString());

            }
        });

    }

    @Override
    public void reg(String u, String p, String t, int roleid, String email, AmicoolListener l) {

    }

    @Override
    public void getList(String mod, int page, final AmicoolListener l) {
        switch (mod){
            case "video":
                Call<List<VideoBean>> call =
                        service.getVideoList(mod,page,"2ts2j910hv4m270hipd\n" +
                                "e4a67d7");
                call.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
        }

    }

    @Override
    public void getDetail(String mod, int id, String s, AmicoolListener l) {

    }
}
